package br.com.dlm.simuladoitil.util;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import br.com.dlm.simuladoitil.R;
import br.com.dlm.simuladoitil.basicas.Categoria;

public class ListaCategoriasAdapter extends BaseAdapter {

    private List<Categoria> categorias;

    public ListaCategoriasAdapter(List<Categoria> categorias) {
        this.categorias = categorias;
    }

    @Override
    public int getCount() {
        return categorias.size();
    }

    @Override
    public Object getItem(int position) {
        return categorias.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Categoria categoria = categorias.get(position);

        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_lista_categorias, null);
            holder = new ViewHolder();
            holder.textViewNomeCategoria = (TextView) convertView.findViewById(R.id.textViewNomeCategoria);
            holder.textViewQuestoesRespondidas = (TextView) convertView.findViewById(R.id.textViewQuestoesRespondidas);
            holder.textViewDescricaoCategoria = (TextView) convertView.findViewById(R.id.textViewDescricaoCategoria);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.textViewNomeCategoria.setText(categoria.nome);
        holder.textViewQuestoesRespondidas.setText(categoria.getQuantidadeQuestoesRespondidasFormatada());
        holder.textViewDescricaoCategoria.setText(categoria.descricao);

        return convertView;
    }

    private class ViewHolder {
        TextView textViewNomeCategoria;
        TextView textViewQuestoesRespondidas;
        TextView textViewDescricaoCategoria;
    }
}
